//
//  WPXMPPTools.m
//  WeChat
//
//  Created by wpapple on 16/6/14.
//  Copyright © 2016年 红红火火恍恍惚惚嘿嘿嘿. All rights reserved.
//

#import "WPXMPPTools.h"
//#import "XMPPFramework.h"
#import "WPUserInfo.h"

@interface WPXMPPTools()<XMPPStreamDelegate>{
    XMPPStream * _xmppStream;
    
    XMPPStateBlock _xmppStateBlock;
    
    XMPPvCardCoreDataStorage *_vCardStorage;
//    头像模块
    XMPPvCardAvatarModule *_vCardAvatar;
}

-(void)setupXMPPStream;

-(void)connectToHost;

-(void)sendPwdToHost;

-(void)sendOnlineToHost;

@end

@implementation WPXMPPTools

singleton_implementation(WPXMPPTools)

#pragma mark -
#pragma mark 外部调用xmpp登录
-(void)xmppLogin:(XMPPStateBlock)stateBlock{
    [_xmppStream disconnect];
    _xmppStateBlock = stateBlock;
    [self connectToHost];
    NSLog(@"-----------");
}

#pragma mark 外部调用xmpp下线
-(void)goOffline{
    XMPPPresence * presence = [XMPPPresence presenceWithType:@"unavailable"];
    
    [_xmppStream sendElement:presence];
    
    [_xmppStream disconnect];
    
//    UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    
//    self.window.rootViewController = storyboard.instantiateInitialViewController;
}

-(void)setupXMPPStream{
    
    _xmppStream = [[XMPPStream alloc]init];
    
    _vCardStorage = [XMPPvCardCoreDataStorage sharedInstance];
    _vCardModule = [[XMPPvCardTempModule alloc] initWithvCardStorage:_vCardStorage];
    //激活
    [_vCardModule activate:_xmppStream];
    
    //添加头像模块
    _vCardAvatar = [[XMPPvCardAvatarModule alloc] initWithvCardTempModule:_vCardModule];
    //激活
    [_vCardAvatar activate:_xmppStream];
    
    [_xmppStream addDelegate:self delegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
}

-(void)connectToHost{
    NSLog(@"开始连接到服务器");
    if (!_xmppStream) {
        [self setupXMPPStream];
    }
    
    NSString * userName = [WPUserInfo sharedWPUserInfo].user;
    
    XMPPJID * myJID = [XMPPJID jidWithUser:userName domain:@"eroco.local" resource:@"iphone"];
    _xmppStream.myJID = myJID;
    
    _xmppStream.hostName = @"127.0.0.1";
    
    _xmppStream.hostPort = 5222;
    
    NSError * error = nil;
    
    if (![_xmppStream connectWithTimeout:XMPPStreamTimeoutNone error:&error]) {
        NSLog(@"%@",error);
    }
}

-(void)sendPwdToHost{
    NSLog(@"再发送密码授权");
    NSError * error = nil;
    
    NSString * password = [WPUserInfo sharedWPUserInfo].pwd;
    [_xmppStream authenticateWithPassword:password error:&error];
    if (error) {
        NSLog(@"%@",error);
    }
}

-(void)sendOnlineToHost{
    
    NSLog(@"发送 在线 消息");
    XMPPPresence * presence = [XMPPPresence presence];
    NSLog(@"%@",presence);
    
    [_xmppStream sendElement:presence];
    
}

-(void)xmppStreamDidConnect:(XMPPStream *)sender{
    NSLog(@"与主机连接成功");
    [self sendPwdToHost];
}

-(void)xmppStreamDidDisconnect:(XMPPStream *)sender withError:(NSError *)error{
    NSLog(@"与主机断开连接 %@",error);
    
    if (_xmppStream && error) {
        _xmppStateBlock(XMPPNetError);
    }
}

-(void)xmppStreamDidAuthenticate:(XMPPStream *)sender{
    NSLog(@"授权成功");
    [self sendOnlineToHost];
    
    _xmppStateBlock(XMPPLoginSuccess);
    
    [WPUserInfo sharedWPUserInfo].loginStatus = YES;
    
    [[WPUserInfo sharedWPUserInfo] saveUserInfoToSandbox];
}


@end
